AddCSLuaFile( "cl_init.lua" ) 
AddCSLuaFile( "cl_targetid.lua" ) 
AddCSLuaFile( "cl_antiradar.lua" )

include( 'shared.lua' )
include( 'player.lua' )

GM.PlayerSpawnTime = {}
GM.fakebarrel = {}
GM.fakebarrelpos = {}
GM.entsloaded = false
GM.nextreset = CurTime() + 10


/*---------------------------------------------------------
   Name: gamemode:Initialize( )
   Desc: Called immediately after starting the gamemode 
---------------------------------------------------------*/
function GM:Initialize( )
end


/*---------------------------------------------------------
   Name: gamemode:InitPostEntity( )
   Desc: Called as soon as all map entities have been spawned
---------------------------------------------------------*/
function GM:InitPostEntity( )
	boxes = ents.FindByModel("models/props_c17/oildrum001_explosive.mdl")  
	for k, v in pairs(boxes) do
		--v:SetCollisionGroup(COLLISION_GROUP_WEAPON)
		table.insert(self.fakebarrel, v)
		table.insert(self.fakebarrelpos, v:GetPos())
		v:SetPos(v:GetPos()+Vector(0,0,20))
		v:DropToFloor()
	end
	self.entsloaded = true
end

/*---------------------------------------------------------
   Name: gamemode:Think( )
   Desc: Called every frame
---------------------------------------------------------*/
function GM:Think( ) -- Changed in version 9 by Hibame

	if(!self.entsloaded) then
		self.entsloaded = false
		for k, v in self.fakebarrel do
			v:SetPos(self.fakebarrelpos[k])
		end
		self.entsloaded = true
	end
	
	if(self.entsloaded) then
		/*---------------------------------------------------------
		if(math.random(100000) < 10) then
			local reseted = math.random(table.maxn(self.fakebarrel))
			self.fakebarrel[reseted]:SetPos(self.fakebarrelpos[reseted])
		end
		---------------------------------------------------------*/
		local x = 0
		while(x < 10) do
			local barrel = math.random(table.maxn(self.fakebarrel)) -- Pick a random barrel from the map
			if(math.random(100) < 11) then
				self.fakebarrel[barrel]:SetAngles(Vector(0,math.random(160),0))
			end
			local barrelmove = self.fakebarrel[barrel]:GetPos() + Vector(math.random(-20,20),math.random(-20,20),20)
			
			local psound = math.random(1000)	
			local TauntSound = Sound( "vo/npc/male01/overhere01.wav" )
			local FootStep1 = Sound( "player/footsteps/tile1.wav" )
			local FootStep2 = Sound( "player/footsteps/tile2.wav" )
			local FootStep3 = Sound( "player/footsteps/tile3.wav" )
			local FootStep4 = Sound( "player/footsteps/tile4.wav" )
			local sound = nil			
			
			if(psound < 2) then
				sound = TauntSound
			elseif(psound == 10) then
			
				local effectdata = EffectData()
				effectdata:SetOrigin( self.fakebarrel[barrel]:GetPos() )
				effectdata:SetNormal( self.fakebarrel[barrel]:GetPos() )
				effectdata:SetMagnitude( 8 )
				effectdata:SetScale( 1 )
				effectdata:SetRadius( 16 )
				util.Effect( "Sparks", effectdata )
		
				self.fakebarrel[barrel]:EmitSound( Sound( "vo/k_lab/kl_ahhhh.wav" ))
		
				// Make an explosion at your position
				local ent = ents.Create( "env_explosion" )
				ent:SetPos( self.fakebarrel[barrel]:GetPos() + Vector(32,0,0))
				ent:Spawn()
				ent:SetOwner( self.fakebarrel )
				ent:SetKeyValue( "iMagnitude", "180" )
				ent:Fire( "Explode", 0, 0 )
				
				self.fakebarrel[barrel]:EmitSound( Sound( "vo/k_lab/kl_ahhhh.wav" ) )
			
			elseif(psound < 400 and psound > 395) then
				sound = FootStep1
			elseif(psound < 600 and psound > 595) then
				sound = FootStep2
			elseif(psound < 800 and psound > 795) then
				sound = FootStep3
			elseif(psound < 1000 and psound > 995) then
				sound = FootStep4
			end
			
			if(sound != nil) then
				self.fakebarrel[barrel]:EmitSound( sound )
			
				self.fakebarrel[barrel]:EmitSound( sound ) -- Needed twice to get the sound to boradcast
			end
			
			//local trace = {}
			//trace.startpos = self.fakebarrel[barrel]:GetPos() -- Find current position
			//trace.endpos = barrelmove -- Find ending position
			//local tracert = util.TraceLine(trace) -- Traces the destinations from start to end and returns a table I believe
			
			local entsnear = ents.FindInSphere(barrelmove, 2)
			if(table.maxn(entsnear) == 0 and util.IsInWorld(barrelmove)) then
				self.fakebarrel[barrel]:SetPos(barrelmove) -- moves the barrel
				//self.fakebarrel[barrel]:SetPos(self.fakebarrelpos[position]) -- another style of moving
				self.fakebarrel[barrel]:DropToFloor()
			end
			x = x+1
		end
	end
end

function GM:Resetbarrels ( )
	for k, v in self.fakebarrel do
		self.fakebarrel[v]:SetPos(self.fakebarrelpos[v])
		Msg("Barrel = " .. k .. " reset\n")
	end
end

--timer.Create(String name, int delay, number reps, function func, ...)
timer.Create("resettimer", 24, 0, Resetbarrels)

function GM:PlayerShouldTakeDamage(ply, owner) -- Should work fine now, not quite sure yet
	if(owner = self.fakebarrel) then
		return false
	else
		return true
	end
end

/*---------------------------------------------------------
   Name: gamemode:EntityKeyValue( ent, key, value )
   Desc: Called when an entity has a keyvalue set
	      Returning a string it will override the value
---------------------------------------------------------*/
function GM:EntityKeyValue( ent, key, value )
end


/*---------------------------------------------------------
   Name: gamemode:ShutDown( )
   Desc: Called when the Lua system is about to shut down
---------------------------------------------------------*/
function GM:ShutDown( )
end

/*---------------------------------------------------------
   Name: gamemode:DoPlayerDeath( )
   Desc: Carries out actions when the player dies 		 
---------------------------------------------------------*/
function GM:DoPlayerDeath( ply, attacker, dmginfo )

	ply:CreateRagdoll()
	
			
	if ( attacker:IsValid() && attacker:IsPlayer() ) then
			
			if ( attacker == ply ) then
			ply:AddDeaths(-1)
			return end
			
			
	
	end
	
end


/*---------------------------------------------------------
   Name: gamemode:EntityTakeDamage( entity, inflictor, attacker, amount )
   Desc: The entity has received damage	 
---------------------------------------------------------*/
function GM:EntityTakeDamage( ent, inflictor, attacker, amount )
end


/*---------------------------------------------------------
   Name: gamemode:OnNPCKilled( entity, attacker, inflictor )
   Desc: The NPC has died
---------------------------------------------------------*/
function GM:OnNPCKilled( ent, attacker, inflictor )

	// Convert the inflictor to the weapon that they're holding if we can.
	if ( inflictor && attacker == inflictor && (inflictor:IsPlayer() || inflictor:IsNPC()) ) then
	
		inflictor = inflictor:GetActiveWeapon()
		if ( attacker == NULL ) then inflictor = attacker end
	
	end
	
	local InflictorClass = "World"
	local AttackerClass = "World"
	
	if ( inflictor && inflictor != NULL ) then InflictorClass = inflictor:GetClass() end
	if ( attacker  && attacker != NULL ) then AttackerClass = attacker:GetClass() end

	if ( attacker && attacker:IsPlayer() ) then
	
		umsg.Start( "PlayerKilledNPC" )
		
			umsg.String( ent:GetClass() )
			umsg.String( InflictorClass )
			umsg.Entity( attacker )
		
		umsg.End()
		
	return end
	
	umsg.Start( "NPCKilledNPC" )
	
		umsg.String( ent:GetClass() )
		umsg.String( InflictorClass )
		umsg.String( AttackerClass )
	
	umsg.End()

end



function KeyPressed (p, key) 
	if ( key == IN_USE ) then 
		if ( p:Team( )== TEAM_BARRELS ) then
		p:Kill( )
		p:SetTeam( TEAM_HUMANS )
		p:PrintMessage(HUD_PRINTTALK,"You're now on the humans team, press 'E' to switch back to the barrels team");
		p:SprintDisable () -- Was enabled
		p:AddFrags(1)
		else
		p:Kill( )
		p:SetTeam( TEAM_BARRELS )
		p:PrintMessage(HUD_PRINTTALK,"You're now on the barrels team, press 'E' to switch back to the humans team");
		p:SprintDisable ()
		p:AddFrags(1)
		end
	end
end
 hook.Add( "KeyPress", "KeyPressedHook", KeyPressed )  





